<?php

namespace App\Service\OpenShop\FeishuMini;

use App\Enums\BaseErrorCode;
use App\Enums\OpenShop\InvestmentPromotion\SiteLocationEnum;
use App\Exceptions\Custom\ResponseHttpException;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\OpenShop\Feishu\PermissionService;
use App\Service\OpenShop\User\RmsUserService;
use App\Service\OpenShop\Wap\SignOrderService;
use GuzzleHttp\Exception\GuzzleException;

class ConsultantsService
{
    const IS_UPDATE_ON  = 1;  //有权限更换
    const IS_UPDATE_OFF = 0;  //无权限更换

    const USER_STATUS_USE  = 0; //在岗
    const USER_STATUS_STOP = 1; //离职

    const USER_STATUS_TEXT = [
        self::USER_STATUS_USE  => '在岗',
        self::USER_STATUS_STOP => '离职',
    ];

    const AVATAR_IMAGE = 'https://file.51kuafu.com/dc/1a/ed20828b5fd6b855662c46f02278f54ec8552953f0a7c37df1ee94777d9f.png'; //默认头像框

    /**
     * 获取门店顾问列表
     * @param $userInfo
     * @param $scope
     * @param $shopId
     * @return array
     * @throws GuzzleException
     */
    public static function lists($userInfo, $allow, $orderNo)
    {
        $model = (new SignOrderService($orderNo, null))->getSignOrderInMerchant();

        //根据mainProcessId查询对应的负责人
        $list = OpenShopOrderManagerModel::query()
            ->where('mainProcessId', $model->mainProcessId)
            ->get();
        //获取用户信息
        $body = RmsUserService::getRmsUserInfoByUserIds($list->pluck('managerUid')->toArray());
        return $list->map(function ($item) use ($allow, $body) {
                    $owner  = '';
                    $mobile = '';
                    $status = self::USER_STATUS_TEXT[self::USER_STATUS_USE];
                    foreach (($body['data'] ?? []) as $user) {
                        if ($user['id'] == $item->managerUid) {
                            $owner  = $user['username'];
                            $mobile = $user['mobile'];
                            $status = self::USER_STATUS_TEXT[$user['status']] ?? '-';
                        }
                    }
                    if (in_array($item->type, $allow)) {
                        $isUpdate = self::IS_UPDATE_ON;
                    } else {
                        $isUpdate = self::IS_UPDATE_OFF;
                    }
                    return [
                        'id'       => $item->id,
                        'isUpdate' => $isUpdate,
                        'name'     => $owner,
                        'mobile'   => $mobile,
                        'position' => $item->description,
                        'status'   => $status,
                        'type'     => $item->type,
                    ];
                })->toArray();
    }

}
